﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManagerWed.Controllers
{
    public class ShenQingController : Controller
    {
        //
        // GET: /ShenQing/
        public ActionResult Index()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");
            return View();
        }     
        
        #region 编辑绑定文本信息
        public JsonResult GetShenqing(int id)
        {
            try
            {
                ShenqingBLL bll = new ShenqingBLL();
                shenqing model = bll.GetModel(id);
                return Json(new { MessageCode = "0", model = model }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
        #region PC模糊查询
        public JsonResult queryshenqing(int pageindex, int pagesize)
        {
            try
            {
                ShenqingBLL bll = new ShenqingBLL();
                List<shenqing> list = bll.GetAgencyBycondtion(pageindex, pagesize);
                object pagecount = bll.GetAgencyBycondtionCount();
                return Json(new { MessageCode = "0", Agencylist = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }
        #endregion
        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int id = Request["id"] != "" ? int.Parse(Request["id"]) : 0;
                if (bll.com_delelte(id, "t_shenqing", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }


        }
        #endregion
        #region 绑定下拉框类别
        public JsonResult GetCateAndLev()
        {
            try
            {
                CategoryBLL Catebll = new CategoryBLL();
                List<category> Catelist = Catebll.GetModelList(" valid=1");
                LevelBLL Levbll = new LevelBLL();
                List<level> Levlist = Levbll.GetModelList(" valid=1");
                return Json(new { MessageCode = "0", Catelist = Catelist, Levlist = Levlist }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
        #region 修改和新增用户，以及授权
        [HttpPost]
        public JsonResult OpeaShenQing()
        {
            try
            {
                bool flag = true;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                int agid = Request["agid"] != null ? int.Parse(Request["agid"].ToString()) : 0;
                string Name = Request["Name"] != null ? Request["Name"] : "";
                string companyName = Request["companyName"] != null ? Request["companyName"] : "";
                string contract_name = Request["contract_name"] != null ? Request["contract_name"] : "";
                string mobile = Request["mobile"] != null ? Request["mobile"] : "";
                string qq = Request["qq"] != null ? Request["qq"] : "";
                string wxOpenid = Request["wxOpenid"] != null ? Request["wxOpenid"] : "";
                string wwCode = Request["wwCode"] != null ? Request["wwCode"] : "";
                string idCard = Request["idCard"] != null ? Request["idCard"] : "";
                string shopUrl = Request["shopUrl"] != null ? Request["shopUrl"] : "";
                int category_id = Request["category_id"] != null ? int.Parse(Request["category_id"]) : 0;
                int level_id = Request["level_id"] != null ? int.Parse(Request["level_id"]) : 0;
                string level_name = Request["level_name"] != null ? Request["level_name"] : "";
                DateTime start_time = Request["start_time"] != null ? DateTime.Parse(Request["start_time"]) : DateTime.Parse("1991-01-01");
                string coding = Request["coding"] != null ? Request["coding"] : "";
                int state = Request["state"] != null ? int.Parse(Request["state"].ToString()) : 1;
                int shState = Request["shState"] != null ? int.Parse(Request["shState"].ToString()) : 1;
                DateTime end_time = start_time.AddDays(90);
                AgencyBLL bll = new AgencyBLL();
                if (!bll.GetSingle(agid, Name, mobile, qq, wxOpenid, wwCode, idCard, shopUrl))
                {
                    agency model = new agency();
                    model.id = agid;
                    model.account_id = agid;
                    model.name = Name;
                    model.company_name = companyName;
                    model.contract_name = contract_name;
                    model.mobile = mobile;
                    model.qq = qq;
                    model.wx_openid = wxOpenid;
                    model.ww_code = wwCode;
                    model.id_card = idCard;
                    model.shop_url = shopUrl;
                    model.category_id = category_id;
                    model.level_id = level_id;
                    model.level_name = level_name;
                    model.start_time = start_time;
                    model.state = state;
                    model.shState = shState;
                    model.valid = 1;
                    model.end_time = end_time;
                    account act_model = new account { user_name = mobile, user_pwd = mobile.Substring(5, 6), type = 2, IsAdmin = 0, mobile = mobile, valid = 1, time = DateTime.Now };
                    int agency_id=0;
                    if (opea == "add")
                    {
                        model.parents_id = 0;
                        flag = bll.InsertAgency(model, act_model, out agency_id);

                    }
                    if (flag)
                    {
                        ShenqingBLL SqBll = new ShenqingBLL();
                        shenqing sq = new shenqing { id = id, state = state };
                        SqBll.Update(sq);

                        if (state == 1)
                        {
                            string au_code = "";
                            agency models = bll.GetModelCode(model, coding);
                            if (models == null)
                            {
                                au_code = coding + "0001";

                            }
                            else
                            {
                                string codes = models.au_code;
                                if (codes != "")
                                {
                                    codes = "1" + codes.Substring(1, codes.Length - 1);
                                    string codess = (int.Parse(codes) + 1).ToString();
                                    au_code = coding + codess.Substring(1, codess.Length - 1);

                                }
                                else
                                {
                                    au_code = coding + "0001";

                                }
                            }
                            AuthorizationBLL aubll = new AuthorizationBLL();
                            authorization aumodel = new authorization { agency_id = agency_id, start_time = start_time, end_time = end_time, valid = 1, time = DateTime.Now };
                            aubll.Add(aumodel);//添加授权记录
                            string bgimg = "upload/lfs_img.jpg";
                            string uid = agency_id.ToString();
                            string bid = model.account_id.ToString();
                            string sid = model.level_id.ToString();
                            string uname = model.name;
                            string ssatrdate = ((DateTime)model.start_time).ToString("yyyy/MM/dd");
                            string senddate = ((DateTime)model.end_time).ToString("yyyy/MM/dd");
                            string sidname = level_name;
                            string address = shopUrl;
                            string prams = "bgimg=" + bgimg + "&uid=" + uid + "&bid=" + bid + "&sid=" + sid + "&uname=" + uname + "&ssatrdate=" + ssatrdate + "&senddate=" + senddate + "&sidname=" + sidname + "&address=" + address + "&au_code=" + au_code + "";
                            string url = "http://query.m12315.com/nfsImg.aspx?" + prams;
                            System.Net.WebClient web = new System.Net.WebClient();
                            byte[] bytes = web.DownloadData(url);
                            string str = System.Text.Encoding.GetEncoding("UTF-8").GetString(bytes);

                            if (str == "0")
                            {
                                string img_url = "http://m.nfs.m12315.com/tasking/" + uid + "-" + bid + ".jpeg";
                                flag = bll.UptSq_imgAndCode(agency_id, au_code, img_url);
                            }
                        }
                        else
                        {
                            flag = bll.UptSq_imgAndCode(agency_id, "", "");
                        }

                    }
                    if (flag)
                    {
                        return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);

                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);


                    }
                }
                else
                {

                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);

                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
	}
}